package de.unibi.comet.ac;

/** Factory to create an LCP-Array out of a string and its suffix array. */
public class LcpArrayFactory {
	
	/** Very naive LCP creation. */
	public static int[] create(String s, int[] suffix_array) {
		int [] lcp = new int[s.length()];
		lcp[0]=0;
		int k = 0;
		for (int i=1; i<suffix_array.length; i++){
			k = 0;
			int n1=suffix_array[i-1];
			int n2=suffix_array[i];
			while ((n1<suffix_array.length) && (n2<suffix_array.length)) {
				if ((s.charAt(n1)==s.charAt(n2)) && !(s.charAt(n1)=='$')) {
					k+=1;
					n1+=1;
					n2+=1;
				} else {
					break;
				}
				lcp[i]=k;
			}
		}
		return lcp;
	}
}
